<template>
  <div class="bankcard-container">
    <div class="bankcard-search">
      <span class="page-title">银行卡列表</span>

      <el-input placeholder="请输入搜索内容" v-model="keyWords" class="input-with-select search-keyword">
        <el-select v-model="select" slot="prepend" placeholder="请选择">
          <el-option label="银行卡号" value="1"></el-option>
          <el-option label="真实姓名" value="2"></el-option>
          <el-option label="身份证号" value="3"></el-option>
        </el-select>
      </el-input>
      <el-button type="primary" size="small" icon="el-icon-search" @click="searchBankCard">搜索</el-button>
      <el-button type="info" size="small" class="search-keyword-reset" plain @click="resetSearch">重置</el-button>
    </div>

    <!--银行卡信息列表-->
    <div class="bankcard-table">
      <el-table ref="multipleTable" v-loading="listLoading" :data="bankData" border stripe fit
                :row-style="{height:'50px'}" :header-row-style="{height:'40px'}"
                :default-sort="{prop: 'time', order: 'descending'}" @sort-change='sortChange'
      >
        <el-table-column label="ID" width="80">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column label="银行卡号" width="180">
          <template slot-scope="scope">
            {{ scope.row.bankNumber }}
          </template>
        </el-table-column>
        <el-table-column label="银行名称" width="320" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.bankName }}
          </template>
        </el-table-column>
        <el-table-column label="真实姓名" width="120">
          <template slot-scope="scope">
            {{ scope.row.userName }}
          </template>
        </el-table-column>
        <el-table-column label="身份证号码" width="180">
          <template slot-scope="scope">
            {{ scope.row.userCardNumber }}
          </template>
        </el-table-column>
        <el-table-column label="生成时间" width="160" sortable prop="time">
          <template slot-scope="scope">
            <span v-if="scope.row.time">{{ scope.row.time | formatTime}}</span>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" min-width="150">
          <template slot-scope="{row}">
            <el-dropdown size="mini" :split-button="true" type="primary" @click="handleLook(row)"
                         v-if="checkPermissionPoints(3) == 1">查看
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(row)" v-if="checkPermissionPoints(4) == 1">编辑
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(row)" v-if="checkPermissionPoints(2) == 1">删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :type="1" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                  @pagination="getBankCardList"/>
    </div>

    <!--查看-->
    <el-dialog title="详细信息" :visible.sync="dialogLookVisible" width="960px" class="bank-dialog look-dialog" top="11vh">
      <el-form ref="form" label-width="120px" size="mini">
        <el-form-item label="用户ID：">
          <span>{{bankCard.id}}</span>
        </el-form-item>
        <el-form-item label="银行卡号：">
          <span>{{bankCard.bankNumber}}</span>
        </el-form-item>
        <el-form-item label="银行名称：">
          <span>{{bankCard.bankName}}</span>
        </el-form-item>
        <el-form-item label="卡种：">
          <span>{{bankCard.cardType}}</span>
        </el-form-item>
        <el-form-item label="银行卡归属地：">
          <span>{{bankCard.bankAddress}}</span>
        </el-form-item>
        <el-form-item label="真实姓名：">
          <span>{{bankCard.userName}}</span>
        </el-form-item>
        <el-form-item label="身份证号码：">
          <span>{{bankCard.userCardNumber}}</span>
        </el-form-item>
        <el-form-item label="员工入职来源：">
          <span>{{bankCard.remark}}</span>
        </el-form-item>
        <el-form-item label="是否隐藏：">
          <span>{{bankCard.delStatus | changeDelStatus}}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" style="width: 96px" @click="dialogLookVisible = false">取消</el-button>
      </div>
    </el-dialog>

    <!--编辑-->
    <el-dialog title="编辑" :lock-scroll="true" :visible.sync="dialogEditVisible" width="960px" class="bank-dialog"
               top="11vh" @close="cancelBankCard">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto" size="mini">
        <el-form-item label="用户ID：">
          <span>{{ruleForm.id}}</span>
        </el-form-item>
        <el-form-item label="银行卡号：" prop="bankNumber">
          <el-input v-model="ruleForm.bankNumber" style="width: 640px;" placeholder="请输入银行卡号"
                    :clearable="true"></el-input>
        </el-form-item>
        <el-form-item label="银行名称：" prop="bankName">
          <el-input v-model="ruleForm.bankName" style="width: 640px;" placeholder="请输入银行名称"
                    :clearable="true"></el-input>
        </el-form-item>
        <el-form-item label="卡种：">
          <span>{{ruleForm.cardType}}</span>
        </el-form-item>
        <el-form-item label="银行卡归属地：" prop="bankAddress">
          <el-input v-model="ruleForm.bankAddress" style="width: 640px;" placeholder="请输入银行卡归属地"
                    :clearable="true"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名：" prop="userName">
          <el-input v-model="ruleForm.userName" style="width: 640px;" placeholder="请输入真实姓名"
                    :clearable="true"></el-input>
        </el-form-item>
        <el-form-item label="身份证号码：" prop="userCardNumber">
          <el-input v-model="ruleForm.userCardNumber" style="width: 640px;" placeholder="请输入身份证号码"
                    :clearable="true"></el-input>
        </el-form-item>
        <el-form-item label="员工入职来源：" prop="remark">
          <el-radio-group v-model="ruleForm.remark">
            <el-radio label="DH">DH</el-radio>
            <el-radio label="LP">LP</el-radio>
            <el-radio label="SS">SS</el-radio>
            <el-radio label="DYH">DYH</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否隐藏：" prop="delStatus">
          <el-radio-group v-model="ruleForm.delStatus">
            <el-radio :label="0">显示</el-radio>
            <el-radio :label="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="success" style="width: 96px" @click="saveBankCard" :loading="sendLoad">保存
        </el-button>
        <el-button size="small" style="width: 96px" @click="cancelBankCard">取消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import Pagination from '@/components/Pagination';
  import {mapGetters} from 'vuex';
  import {permissionPoints} from '@/utils/permission';
  import {formatDate} from "@/utils/index";
  import {deleteBankInfo, getBankInfo, getInitBankCardList, modifyBankInfo} from "../../../api/user/user";

  export default {
    name: "bankCardManage",
    computed: {
      ...mapGetters([
        'points'
      ])
    },
    components: {
      Pagination
    },
    data() {
      return {
        total: 0,
        listQuery: {
          page: 1,
          limit: 20,
          bankNumber: '',
          userName: '',
          userCardNumber: '',
          orderBy: ''
        },
        listLoading: true,
        select: '1',
        keyWords: '',
        bankData: [],
        bankCard: {},
        dialogLookVisible: false,
        dialogEditVisible: false,
        ruleForm: {
          id: '',
          bankNumber: '',
          bankName: '',
          bankAddress: '',
          userName: '',
          userCardNumber: '',
          remark: '',
          delStatus: '',
          cardType: '',
          userId: ''
        },
        rules: {
          bankNumber: [
            {required: true, message: '请输入银行卡号', trigger: 'blur'}
          ],
          bankName: [
            {required: true, message: '请输入银行名称', trigger: 'blur'}
          ],
          bankAddress: [
            {required: true, message: '请输入银行归属地', trigger: 'blur'}
          ],
          userName: [
            {required: true, message: '请输入真实姓名', trigger: 'blur'}
          ],
          userCardNumber: [
            {required: true, message: '请输入身份证号码', trigger: 'blur'}
          ],
          remark: [
            {required: true, message: '请选择员工入职来源', trigger: 'change'}
          ],
          delStatus: [
            {required: true, message: '请选择是否隐藏', trigger: 'change'}
          ]
        },
        sendLoad: false
      }
    },
    created() {
      this.getBankCardList();
    },
    methods: {
      searchBankCard() {//搜索
        if (this.keyWords == '' || this.keyWords == undefined) {
          return this.$message.info({message: '请输入搜索内容', duration: 1000});
        }
        this.listQuery.bankNumber = '';
        this.listQuery.userName = '';
        this.listQuery.userCardNumber = '';
        if (this.select == 1) {
          this.listQuery.bankNumber = this.keyWords;
        } else if (this.select == 2) {
          this.listQuery.userName = this.keyWords;
        } else if (this.select == 3) {
          this.listQuery.userCardNumber = this.keyWords;
        }
        this.listQuery.page = 1;
        this.getBankCardList();
      },
      resetSearch() {
        this.select = '1';
        this.keyWords = '';
        this.listQuery.bankNumber = '';
        this.listQuery.userName = '';
        this.listQuery.userCardNumber = '';
        this.listQuery.page = 1;
        this.getBankCardList();
      },
      getBankCardList() {
        this.listLoading = true;
        getInitBankCardList(this.listQuery).then((res) => {
          this.listLoading = false;
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            const {items, totalRecord} = retObj.data;
            this.total = totalRecord;
            this.bankData = items;
          }
        }).catch(() => {
          this.listLoading = false;
        })
      },
      checkPermissionPoints(type) {
        let pointObj = this.points.bankCardManage;
        return permissionPoints(type, pointObj);
      },
      sortChange(column) {//排序
        if (column.prop == "time" && column.order == "ascending") {
          this.listQuery.orderBy = 'time asc';
        } else if (column.prop == "time" && column.order == "descending") {
          this.listQuery.orderBy = "time desc";
        }
        this.listQuery.page = 1;
        this.getBankCardList();
      },
      handleLook(row) {
        this.dialogLookVisible = true;
        getBankInfo(row.id).then((res) => {
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            this.bankCard = retObj.data;
          }
        })
      },
      handleEdit(row) {
        this.dialogEditVisible = true;
        getBankInfo(row.id).then((res) => {
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            this.ruleForm = retObj.data;
          }
        })
      },
      cancelBankCard() {
        this.dialogEditVisible = false;
        this.$refs.ruleForm.resetFields();
      },
      saveBankCard() {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.sendLoad = true;
            modifyBankInfo(this.ruleForm).then((res) => {//修改
              let retObj = res;
              this.sendLoad = false;
              if (retObj.code == 0 && retObj.data == 1) {
                this.ruleForm = {};
                this.dialogEditVisible = false;
                this.$message.success({message: '修改成功', duration: 2000});
                this.getBankCardList();
              } else {
                this.$message.error({message: '修改失败，请重试', duration: 2000});
              }
            }).catch(() => {
              this.sendLoad = false;
            })
          } else {
            return false;
          }
        });
      },
      handleDelete(row) {
        this.$confirm('此操作将删除该银行卡, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteBankInfo(row.id).then((res) => {
            let retObj = res;
            if (retObj.code == 0 && retObj.data > 0) {
              this.$message.success({message: '删除成功', duration: 2000});
              this.getBankCardList();
            }
          })
        }).catch(() => {
        });
      }
    },
    filters: {
      formatTime(time) {
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss');
      },
      changeDelStatus(type) {
        if (type == 0) {
          return '显示';
        } else if (type == 1) {
          return '隐藏';
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .bankcard-container {
    .bankcard-search {
      display: flex;
      align-items: center;
      width: 100%;
      height: 80px;
      .page-title {
        padding-left: 20px;
        font-size: 18px;
        font-weight: bold;
        min-width: 120px;
        text-align: left;
        color: rgba(61, 68, 77, 1);
      }
      .search-keyword {
        margin-left: 36px;
        margin-right: 24px;
        width: 470px;
        > > > .el-select .el-input {
          width: 110px;
        }
        > > > .el-input-group__prepend {
          background-color: #fff;
        }
      }
      .search-keyword-reset {
        margin-right: 36px;
      }
    }
    .bankcard-table {
      width: 98%;
      margin-left: 18px;
      > > > .el-table td, .el-table th {
        padding: 6px 0px;
      }
      > > > .el-table__header th {
        padding: 0;
        height: 30px;
        line-height: 30px;
      }
      > > > .el-table__column-filter-trigger {
        line-height: 28px;
        i {
          font-size: 18px;
        }
      }
      > > > .el-table__row .cell {
        display: flex;
        align-items: center;
      }
      > > > .el-table__header .has-gutter .cell {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      > > > .el-table__fixed-right {
        height: 100% !important;
      }
    }
    .bank-dialog > > > .el-dialog .el-dialog__header {
      display: flex;
      align-items: center;
      height: 48px;
      background: #F7F8FA;
      padding: 0px 0px 0px 12px;
      .el-dialog__headerbtn {
        top: 15px;
      }
    }
    .look-dialog > > > .el-form-item--mini.el-form-item, .el-form-item--small.el-form-item {
      margin-bottom: 0px;
    }
    .bank-dialog {
      font-size: 14px;
      color: #606266;
    }
    .bank-dialog > > > .el-input--mini .el-input__inner {
      background: #F7F8FA;
    }
    > > > .el-button {
      line-height: 1.2;
    }
  }
</style>
